<?php 
namespace app\index\taglib;

use think\template\TagLib;

class ebcms extends Taglib
{

    // 标签定义
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'test'          => ['attr' => 'name'],
        'nav'           => ['attr' => 'mark,tree,pid,limit,return', 'close' => 0],
        'position'      => ['attr' => 'return', 'close' => 0],
        'config'        => ['attr' => 'name', 'close' => 0],
    ];

    public function tagtest($tag, $content)
    {
        $parseStr = '__test__ ' . $content . ' __ test__';
        return $parseStr;
    }

    public function tagconfig($tag, $content)
    {
        if (empty($tag['name'])) {
            return;
        }
        $str = '';
        $str .="<?php ";
        if ('$' == substr($tag['name'],0,1)) {
            $tag['name']   = $this->autoBuildVar($tag['name']);
            $str .="echo \\ebcms\\Config::get('custom.'.{$tag['name']});";
        }else{
            $str .="echo \\ebcms\\Config::get('custom.{$tag['name']}');";
        }
        $str .=" ?>";
        return $str;
    }
    
    public function tagposition($tag, $content)
    {
        if (empty($tag['return'])) {//return为空 返回空
            $tag['return'] = '_data';
        }
        $str = '';
        $str .="<?php ";
        $str .="\${$tag['return']} = \\ebcms\\Position::get();";
        $str .=" ?>";
        return $str;
    }

    public function tagnav($tag,$content) {
        if (empty($tag['mark'])) {//mark为空 返回空
            return;
        }
        if (empty($tag['return'])) {//return为空 返回空
            $tag['return'] = '_data';
        }
        $str = '';
        $str .="<?php ";
        $str .="\${$tag['return']} = [];";
        $str .="\$_m = \\app\\admin\\model\\Nav::with('navcate');";
        $str .="\$_where = [];";
        $str .="\$_where['nav.status'] = ['eq',1];";
        $str .="\$_where['navcate.status'] = ['eq',1];";
        // 解析 mark 限制
        if ('$' == substr($tag['mark'],0,1)) {
            $tag['mark']   = $this->autoBuildVar($tag['mark']);
            $str .="\$_where['navcate.mark'] = ['eq',{$tag['mark']}];";
        }else{
            $str .="\$_where['navcate.mark'] = ['eq','{$tag['mark']}'];";
        }
        $str .="\$_m -> order('nav.sort desc,nav.id asc');";
        // 解析 tree 限制
        if (isset($tag['tree']) && $tag['tree'] == 1) {
            $str .="\${$tag['return']}_tmp = \$_m -> where(\$_where) -> select();";
            // pid限制
            if (isset($tag['pid'])) {
                if ('$' == substr($tag['pid'],0,1)) {
                    $tag['pid']   = $this->autoBuildVar($tag['pid']);
                    $str .="\${$tag['return']} = \\ebcms\\Tree::subtree(\${$tag['return']}_tmp,{$tag['pid']});";
                }else{
                    $str .="\${$tag['return']} = \\ebcms\\Tree::subtree(\${$tag['return']}_tmp,'{$tag['pid']}');";
                }
            }else{
                $str .="\${$tag['return']} = \\ebcms\\Tree::tree(\${$tag['return']}_tmp);";
            }
        }else{
            // 解析limit限制属性
            if (isset($tag['limit'])) {
                if ('$' == substr($tag['limit'],0,1)) {
                    $tag['limit']   = $this->autoBuildVar($tag['limit']);
                    $str .="\$_m -> limit({$tag['limit']});";
                }else{
                    $str .="\$_m -> limit('{$tag['limit']}');";
                }
            }
            // pid限制
            if (isset($tag['pid'])) {
                if ('$' == substr($tag['pid'],0,1)) {
                    $tag['pid']   = $this->autoBuildVar($tag['pid']);
                    $str .="\$_where['nav.pid'] = array('eq',{$tag['pid']});";
                }else{
                    $str .="\$_where['nav.pid'] = array('eq','{$tag['pid']}');";
                }
            }
            $str .="\$_m -> where(\$_where);";
            $str .="\${$tag['return']} = \$_m -> select();";
        }
        $str .=" ?>";
        return $str;
    }

}